// 接口方法
import request from '@/utils/request'
import { getToken } from '@/utils/token'

// 获取列表接口
export const getChannelListAPI = () => request({ url: '/v1_0/channels' })
/**
 * 登录接口
 * @param {*} param0 数据对象
 * @returns Promise对象
 */

// 登录接口
export const loginApi = ({ mobile, code }) => request({
  url: '/v1_0/authorizations',
  method: 'POST',
  data: {
    mobile,
    code
  }
})

// 获取用户频道
export const getUserChannelListAPI = () => request({
  url: '/v1_0/user/channels'
})

// 获取文章新闻推荐 - 首页内容
export const getArticleListAPI = ({ channel_id, timestamp = new Date() }) =>

  request({
    url: '/v1_0/articles',
    headers: {
      Authorization: `Bearer ${getToken()}`
    },
    params: {
      channel_id,
      timestamp
    }
  })

// 对文章不喜欢,不感兴趣
export const dislikeArticleAPI = (artId) => request({
  url: '/v1_0/article/dislikes',
  method: 'POST',

  data: {
    target: artId
  }
})

// 举报文章-接口
export const reportArticleAPI = ({ artId, type }) => request({
  url: '/v1_0/article/reports',
  method: 'POST',

  data: {
    target: artId,
    type: type,
    remark: '就是其它问题'

  }
})

// 获取文章详情
export const getArticledetailAPI = ({ artId }) => request({
  url: `/v1_0/articles/${artId}`
})

// 设置用户频道(重置式)-接口
export const setUserChannelAPI = (channelArr) => request({
  url: '/v1_0/user/channels',
  method: 'PUT',
  data: {
    channels: channelArr
  }
})

// 删除频道-接口
export const removeChannelAPI = (channnelId) => request({
  url: `/v1_0/user/channels/${channnelId}`,
  method: 'DELETE',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded'
  }
})

// 搜索-建议联想列表-接口
export const suggestListAPI = (keyWord) => request({
  url: '/v1_0/suggestion',
  // method: 'GET',
  params: {
    q: keyWord
  }
})

// 搜索 - 结果列表
export const searchResultAPI = ({ page = 1, per_page = 100, keyWord }) => request({
  url: '/v1_0/search',
  // method: 'GET',
  params: {
    q: keyWord,
    page: page,
    per_page: per_page
  }
})

// 点击关注/取关作者
export const followedAuthorAPI = (autId) => request({
  url: '/v1_0/user/followings',
  method: 'POST',
  data: {
    target: autId
  }
})
export const unFollowedAuthorAPI = (autId) => request({
  url: `/v1_0/user/followings/${autId}`,
  method: 'DELETE'
})

// 点赞/取消点赞作者
export const likeAuthorAPI = (artId) => request({
  url: '/v1_0/article/likings',
  method: 'POST',
  data: {
    target: artId
  }
})
export const disLikeAuthorAPI = (artId) => request({
  url: `/v1_0/article/likings/${artId}`,
  method: 'DELETE'
})

// 对评论点赞/取消点赞
export const likeCommentsAPI = (com_id) => request({
  url: '/v1_0/comment/likings',
  method: 'POST',
  data: {
    target: com_id
  }
})
export const disLikeCommentsAPI = (com_id) => request({
  url: `/v1_0/comment/likings/${com_id}`,
  method: 'DELETE'
})

// 获取评论接口
export const getCommentsListAPI = ({ artId, offset = null, limit = 10 }) => request({
  url: '/v1_0/comments',
  // method: 'DELETE'
  params: {
    type: 'a',
    source: artId,
    offset: offset,
    limit: limit
  }
})

// 评论 - 发布评论
export const sendCommentAPI = ({ target, content, artId = null }) => {
  const obj = {
    target: target, // 文章Id 或 评论Id
    content: content
  }
  // 判断是否是对评论回复 是则需要携带文章id
  // 对文章评论不需要id
  if (artId !== null) {
    obj.art_id = artId
  }
  return request({
    url: '/v1_0/comments',
    method: 'POST',
    data: obj

  })
}

// 获取用户信息
export const getUserInfoAPI = () => request({
  url: '/v1_0/user'
})

// 获取用户个人资料(头像,生日,名称)
export const getProfileAPI = () => request({
  url: '/v1_0/user/profile'
})

// 更新个人头像
export const uploadHeadImgAPI = (fd) => request({
  url: '/v1_0/user/photo',
  method: 'PATCH', // 局部更新
  data: fd
})

// 更新个人信息
export const updateUserInfoAPI = ({
  name = null,
  gender = null,
  birthday = null,
  real_name = null,
  intro = null
}) => {
  const obj = {}
  if (name !== null) { obj.name = name }
  if (gender !== null) { obj.gender = gender }
  if (birthday !== null) { obj.birthday = birthday }
  if (real_name !== null) { obj.real_name = real_name }
  if (intro !== null) { obj.intro = intro }
  return request({
    url: '/v1_0/user/profile',
    method: 'PATCH',
    data: obj
  })
}

// 刷新用户token
export const refreshTokenAPI = () => request({
  url: '/v1_0/authorizations',
  method: 'PUT',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
    Authorization: 'Bearer ' + localStorage.getItem('refresh_token')
  }
})
// 接口方法, 只负责调用一个接口, 返回一个Promise对象
// export const allChannelListAPI = () => {
//   return axios({
//     url: '/v1_0/channels'
//   })
// }
